var em = new Vue({
    el: '#app',
    data: {
        auditPassCount: '',
        toAuditCount: '',
        auditNoPassCount: '',
        productName: '',
        productCategory: '',
        userAccount: ''
    },
    created(){
        var self = this;

        //给按钮 审核通过 赋值
        $.ajax({
            url: '/SyCourseRecord/getProductsCount',
            dataType: 'json',
            type: 'post',
            data:{'result':1},
            success:function (data) {
                self.auditPassCount = data.data;
            }
        });
        //给按钮 未通过 赋值
        $.ajax({
            url: '/SyCourseRecord/getProductsCount',
            dataType: 'json',
            type: 'post',
            data:{'result':3},
            success:function (data) {
                self.auditNoPassCount = data.data;
            }
        });
        //给按钮 待审核 赋值
        $.ajax({
            url: '/SyCourseRecord/getProductsCount',
            dataType: 'json',
            type: 'post',
            data:{'result':2},
            success:function (data) {
                self.toAuditCount = data.data;
            }
        });

    }

});
layui.use(['jquery','layer','table','form'],function(){
    var $=layui.jquery;
    var layer=layui.layer;
    var table=layui.table;
    var form=layui.form;
    $(document).click(function () {
        parent.$('.tab').hide();
    });
    $.ajax({
        url: '/SyStatistical/getSyProductCategroys',
        dataType: 'json',
        type: 'post',
        success: function (data) {
            var syProductCategroys = data.data;
            for (var i = 0; i < syProductCategroys.length; i++) {
                $('#title').append('<option value="' + syProductCategroys[i].title + '" >' + syProductCategroys[i].title + '</option>')
            }
            form.render('select');
        }
    });
    //状态按钮切换效果
    $('.buttonBox').children('.layui-btn').click(function () {
        $(this).removeClass('layui-btn-primary').siblings().addClass('layui-btn-primary');
    });
    //高级检索按钮
    $('#advancedSearching').click(function () {
        layer.open({
            type: 2,
            title: '高级检索',
            content: 'product-sample.html',
            area: ['690px', '385px'],
            offset: 'auto',
            shade: 0.4,
            end: function () {
                $('.buttonBox .layui-btn').addClass('layui-btn-primary');
            }
        })
    });
    //返回按钮
    $('#goBack').click(function () {
        var iframe = parent.$('.iframe');
        $.each(iframe,function (i,item) {
            if('商品审核列表' == $(item).attr('_href')){
                parent.$('#beforeHref').val($(item).attr('_href'));
                parent.beforeHref();
                return;
            }
        });
    });
    refresh=function refresh(){
        location.reload();
    }

    //审核通过按钮
    $('#auditPassButton').click(function () {
        getRecordResultProducts("1");
    });
    //待审核按钮
    $('#toAuditButton').click(function () {
        getRecordResultProducts("2");
    });
    //未通过按钮
    $('#auditNoPassButton').click(function () {
        getRecordResultProducts("3");
    });
    //根据不同的审核状态查询商品
    function getRecordResultProducts(result){
        var products = {
            result: result,
            type: 3
        }
        $.ajax({
            url: '/product/getRecordResultProducts',
            data: products,
            dataType: 'json',
            type: 'post',
            success: function (data) {
                auditTableRender(data.data.data);
            }
        });
    }
    //获取表中数据
    getAuditTable('','','');
    function getAuditTable(productName,title,userAccount){
        $.ajax({
            url: '/product/getAuditTableAll',
            dataType: 'json',
            data: {
                type: 3,
                productName: productName,
                title: title,
                userAccount: userAccount
            },
            type: 'post',
            success:function (data) {
                auditTableRender(data.data.data);
            }
        });
    }
    //查询按钮点击事件
    $('#search').click(function () {
        var productName = $('#productName').val();
        var title=$("#title option:selected").text();
        var userAccount = $('#userAccount').val();
        getAuditTable(productName,title,userAccount);
        $('.buttonBox .layui-btn').addClass('layui-btn-primary');
    });
    /*表格的渲染*/
    auditTableRender = function auditTableRender(data){
        table.render({
            elem:'#productTable',
            title:"商品列表",
            data: data,
            toolbar:'#button_ids',
            limit:10,
            width:1620,
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                limits:[10,20,30],
                first:"首页",
                last:"尾页",
            },
            cols:[[
                {title:'序号',align:'center',width:60,templet:function(data){
                        return data.LAY_INDEX;
                    }},
                {field:"productCode",title:"商品编号",align:'center',width:175},
                {field:"",title:"商品图片",align:'center',width:175,templet:function(data){
                        return '<img src="'+ data.mainImage +'">';
                    }},
                {field:"productName",title:"商品名称",align:'center',width:175},
                {field:"originalpriceProduct",title:"原价",align:'center',width:175,templet:function(data){
                        return '原价：' + data.originalpriceProduct;
                    }},
                {field:"currentpriceProduct",title:"现价",align:'center',width:175,templet:function(data){
                        return '现价：' + data.currentpriceProduct;;
                    }},
                {field:"title",title:"分类",align:'center',width:175},
                {field:"userAccount",title:"上传人员",align:'center',width:175},
                {field:"",title:"审核状态",align:'center',width:175,templet:function(data){
                        if(data.result == 2){
                            return '<p>待审核</p>';
                        }else if(data.result == 1){
                            return '<p>审核通过</p>';
                        }else if(data.result == 3){
                            return '<p>未通过</p>';
                        }
                    }},
                {field:'',title:"操作",align:'center',width:148,templet:function(data){
                        if(data.result == 1 || data.result == 3){
                            return '<a alt="查看" title="查看" href="javascript:;" style="margin-right: 10px" lay-event="selectTable">查看</a>' + '<p></p>';
                        }else if(data.result == 2){
                            return '<a alt="查看" title="查看" href="javascript:;"  style="margin-right: 10px" lay-event="selectTable">查看</a>' +
                                '<a alt="审核" title="审核" href="javascript:;" lay-event="check">审核</a>';
                        }
                    }}
            ]]
        })
        //行内工具栏事件
        table.on('tool(productTable)',function (obj) {
            if(obj.event == 'selectTable'){
                layer.open({
                    type:2,
                    title:'查看商品信息',
                    content:'product-audit-select-message.html',
                    btn:["确定","取消"],
                    area:["95%","99%"],
                    scrollbar: false,
                    offset:'auto',
                    shade:0.4,
                    success:function(layero,index) {
                        var childHtml = layero.find('iframe').contents();
                        childHtml.find('#productCode').text(obj.data.productCode);
                        childHtml.find('#productName').text(obj.data.productName);
                        childHtml.find('#currentpriceProduct').text(obj.data.currentpriceProduct);
                        childHtml.find('#categoryTitle').text(obj.data.title);
                        var result = obj.data.result;
                        var newResult;
                        if(result == 1){
                            newResult = '审核通过';
                        }else if(result == 2){
                            newResult = '待审核';
                        }else if(result == 3){
                            newResult = '审核不通过';
                        }
                        childHtml.find('#recordResult').text(newResult);
                        childHtml.find('#searchKeywords').text(obj.data.searchKeywords);
                        childHtml.find('#userAccount').text(obj.data.userAccount);
                        childHtml.find('#createDate').text(obj.data.createDate);
                        childHtml.find('#productDescription').text(obj.data.productDescription);
                        childHtml.find('#mainImage').attr('src',obj.data.mainImage);
                        childHtml.find('#productLabel').text(obj.data.productLabel);

                    }
                });
            }else if(obj.event == 'check'){
                layer.open({
                    type:2,
                    title:'审核商品',
                    content:'product-audit.html',
                    area:['752px','400px'],
                    offset:'auto',
                    shade:0.4,
                    success: function(layero,index){
                        var childHtml = layero.find('iframe').contents();
                        var user = JSON.parse(localStorage.getItem('user'));
                        childHtml.find('#courseId').val(obj.data.productId);
                        childHtml.find('#implementId').val(user.data.userId);
                        childHtml.find('#account').val(user.data.account);
                        childHtml.find('#type').val(3);
                    },
                    end: function () {
                        getRecordResultProducts();
                    }
                });
            }
        });
    }

})